package cn.tedu.basic;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;

//测试网络编程 套接字 --服务器端
public class Sever {
    public static void main(String[] args) throws IOException {
        //开启服务器--并制定端口号
        ServerSocket server =new ServerSocket(12345);//0~65535
        System.out.println("服务器已启动~");
        //接收客户端发来的链接请求
        Socket acc = server.accept();
        System.out.println("服务器收到一个客户端连接请求~");
        //读取数据
        InputStream in = acc.getInputStream();
        int read = in.read();
        char ch = (char) read;
        System.out.println(read);
        System.out.println(ch);
        for (int i = 0; i <10 ; i++) {
            int rea =in.read();
            char c = (char) rea;
            System.out.print(c);
        }
        in.close();
        //给客户端写出I love you too--out
        OutputStream outp = acc.getOutputStream();
        outp.write("I love you too !".getBytes());
        outp.close();
        //读取数据
        InputStream inpu =acc.getInputStream();
        String st ="The day is beautiful !";
        for (int i = 0; i <st.length() ; i++) {
            int read1 = inpu.read();
            char cha= (char) read1;
            System.out.print(cha);
        }
        inpu.close();
        //高级流写入--IO自动关闭
        BufferedInputStream buf =null;
            try{   buf= new BufferedInputStream(acc.getInputStream());
        String str ="I miss you every day !";
        for (int i = 0; i <str.length() ; i++) {
            int read2 = buf.read();
            char c = (char) read2;
            System.out.print(c);
        }
        }catch (Exception a){
                System.out.println("74646");
            }finally {
                buf.close();
            }
        //高级流写出
        BufferedOutputStream bufo =new BufferedOutputStream(acc.getOutputStream());
        bufo.write("fbfgnbhgn".getBytes());
        bufo.close();
    }
}
